package com.lms.nio.channel;


import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-05-06 07:30
 * @Description: ${Description}
 */
public class FileChannel01 {
    public static void main(String[] args) throws Exception{

        FileInputStream fis = new FileInputStream("W:\\学习笔记\\demo.txt");
        FileOutputStream fos  = new FileOutputStream("W:\\学习笔记\\demo2.txt");
        BufferedOutputStream bos = new BufferedOutputStream(System.out);
        FileChannel channel = fis.getChannel();
        FileChannel fosChannel = fos.getChannel();
        ByteBuffer allocate = ByteBuffer.allocate(10);
        while (true){
            allocate.clear();
            int read = channel.read(allocate);
            if (read ==-1){
               break;
            }
            allocate.flip();
            bos.write(allocate.array(),0,read);
            bos.flush();
            fosChannel.write(allocate);

        }
        fosChannel.close(); //关闭通道



    }


}
